﻿using Futuret.Core.Futuret;
using Futuret.Core.Futuret.Interface;
using Futuret.Logger.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace Future.Shared.Core.Extensions;

/// <summary>
///     Author: Riva
///     Date: 2023/7/11 14:17
///     Description:
/// </summary>
public static class WebApplicationBuilderExtension
{
    /// <summary>
    ///     WebApplicationBuilder启动
    /// </summary>
    /// <param name="builder"></param>
    /// <param name="serviceInfo"></param>
    /// <returns></returns>
    public static WebApplicationBuilder ConfigureCloudDefault(this WebApplicationBuilder builder,
        IAppInfo serviceInfo)
    {
        //注入日志
        builder.AddCloudLogger();

        //补齐AppInfo信息
        AppInfo.AddElement(x =>
        {
            x.serviceCollection = builder.Services;
            x.webHostEnvironment = builder.Environment;
            x.configuration = builder.Configuration;
        });

        if (builder == null)
            throw new ArgumentNullException(nameof(builder));
        if (serviceInfo == null)
            throw new ArgumentNullException(nameof(serviceInfo));

        builder.Services.AddSingleton(typeof(IAppInfo), serviceInfo);
        builder.Services.AddCloud(serviceInfo);

        return builder;
    }
}